//
//  ZCInvolvedDetailsModelHotComments.m
//
//  Created by   on 16/3/28
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "ZCInvolvedDetailsModelHotComments.h"
#import "ZCInvolvedDetailsModelChildComments.h"


NSString *const kZCInvolvedDetailsModelHotCommentsId = @"id";
NSString *const kZCInvolvedDetailsModelHotCommentsMid = @"mid";
NSString *const kZCInvolvedDetailsModelHotCommentsCommentCount = @"comment_count";
NSString *const kZCInvolvedDetailsModelHotCommentsIshidden = @"ishidden";
NSString *const kZCInvolvedDetailsModelHotCommentsCtype = @"ctype";
NSString *const kZCInvolvedDetailsModelHotCommentsAvatar = @"avatar";
NSString *const kZCInvolvedDetailsModelHotCommentsCtime = @"ctime";
NSString *const kZCInvolvedDetailsModelHotCommentsLikecount = @"likecount";
NSString *const kZCInvolvedDetailsModelHotCommentsUsername = @"username";
NSString *const kZCInvolvedDetailsModelHotCommentsChildComments = @"child_comments";
NSString *const kZCInvolvedDetailsModelHotCommentsForId = @"for_id";
NSString *const kZCInvolvedDetailsModelHotCommentsPid = @"pid";
NSString *const kZCInvolvedDetailsModelHotCommentsContent = @"content";


@interface ZCInvolvedDetailsModelHotComments ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ZCInvolvedDetailsModelHotComments

@synthesize hotCommentsIdentifier = _hotCommentsIdentifier;
@synthesize mid = _mid;
@synthesize commentCount = _commentCount;
@synthesize ishidden = _ishidden;
@synthesize ctype = _ctype;
@synthesize avatar = _avatar;
@synthesize ctime = _ctime;
@synthesize likecount = _likecount;
@synthesize username = _username;
@synthesize childComments = _childComments;
@synthesize forId = _forId;
@synthesize pid = _pid;
@synthesize content = _content;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.hotCommentsIdentifier = [self objectOrNilForKey:kZCInvolvedDetailsModelHotCommentsId fromDictionary:dict];
            self.mid = [self objectOrNilForKey:kZCInvolvedDetailsModelHotCommentsMid fromDictionary:dict];
            self.commentCount = [self objectOrNilForKey:kZCInvolvedDetailsModelHotCommentsCommentCount fromDictionary:dict];
            self.ishidden = [self objectOrNilForKey:kZCInvolvedDetailsModelHotCommentsIshidden fromDictionary:dict];
            self.ctype = [self objectOrNilForKey:kZCInvolvedDetailsModelHotCommentsCtype fromDictionary:dict];
            self.avatar = [self objectOrNilForKey:kZCInvolvedDetailsModelHotCommentsAvatar fromDictionary:dict];
            self.ctime = [self objectOrNilForKey:kZCInvolvedDetailsModelHotCommentsCtime fromDictionary:dict];
            self.likecount = [self objectOrNilForKey:kZCInvolvedDetailsModelHotCommentsLikecount fromDictionary:dict];
            self.username = [self objectOrNilForKey:kZCInvolvedDetailsModelHotCommentsUsername fromDictionary:dict];
    NSObject *receivedZCInvolvedDetailsModelChildComments = [dict objectForKey:kZCInvolvedDetailsModelHotCommentsChildComments];
    NSMutableArray *parsedZCInvolvedDetailsModelChildComments = [NSMutableArray array];
    if ([receivedZCInvolvedDetailsModelChildComments isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedZCInvolvedDetailsModelChildComments) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedZCInvolvedDetailsModelChildComments addObject:[ZCInvolvedDetailsModelChildComments modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedZCInvolvedDetailsModelChildComments isKindOfClass:[NSDictionary class]]) {
       [parsedZCInvolvedDetailsModelChildComments addObject:[ZCInvolvedDetailsModelChildComments modelObjectWithDictionary:(NSDictionary *)receivedZCInvolvedDetailsModelChildComments]];
    }

    self.childComments = [NSArray arrayWithArray:parsedZCInvolvedDetailsModelChildComments];
            self.forId = [self objectOrNilForKey:kZCInvolvedDetailsModelHotCommentsForId fromDictionary:dict];
            self.pid = [self objectOrNilForKey:kZCInvolvedDetailsModelHotCommentsPid fromDictionary:dict];
            self.content = [self objectOrNilForKey:kZCInvolvedDetailsModelHotCommentsContent fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.hotCommentsIdentifier forKey:kZCInvolvedDetailsModelHotCommentsId];
    [mutableDict setValue:self.mid forKey:kZCInvolvedDetailsModelHotCommentsMid];
    [mutableDict setValue:self.commentCount forKey:kZCInvolvedDetailsModelHotCommentsCommentCount];
    [mutableDict setValue:self.ishidden forKey:kZCInvolvedDetailsModelHotCommentsIshidden];
    [mutableDict setValue:self.ctype forKey:kZCInvolvedDetailsModelHotCommentsCtype];
    [mutableDict setValue:self.avatar forKey:kZCInvolvedDetailsModelHotCommentsAvatar];
    [mutableDict setValue:self.ctime forKey:kZCInvolvedDetailsModelHotCommentsCtime];
    [mutableDict setValue:self.likecount forKey:kZCInvolvedDetailsModelHotCommentsLikecount];
    [mutableDict setValue:self.username forKey:kZCInvolvedDetailsModelHotCommentsUsername];
    NSMutableArray *tempArrayForChildComments = [NSMutableArray array];
    for (NSObject *subArrayObject in self.childComments) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForChildComments addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForChildComments addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForChildComments] forKey:kZCInvolvedDetailsModelHotCommentsChildComments];
    [mutableDict setValue:self.forId forKey:kZCInvolvedDetailsModelHotCommentsForId];
    [mutableDict setValue:self.pid forKey:kZCInvolvedDetailsModelHotCommentsPid];
    [mutableDict setValue:self.content forKey:kZCInvolvedDetailsModelHotCommentsContent];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.hotCommentsIdentifier = [aDecoder decodeObjectForKey:kZCInvolvedDetailsModelHotCommentsId];
    self.mid = [aDecoder decodeObjectForKey:kZCInvolvedDetailsModelHotCommentsMid];
    self.commentCount = [aDecoder decodeObjectForKey:kZCInvolvedDetailsModelHotCommentsCommentCount];
    self.ishidden = [aDecoder decodeObjectForKey:kZCInvolvedDetailsModelHotCommentsIshidden];
    self.ctype = [aDecoder decodeObjectForKey:kZCInvolvedDetailsModelHotCommentsCtype];
    self.avatar = [aDecoder decodeObjectForKey:kZCInvolvedDetailsModelHotCommentsAvatar];
    self.ctime = [aDecoder decodeObjectForKey:kZCInvolvedDetailsModelHotCommentsCtime];
    self.likecount = [aDecoder decodeObjectForKey:kZCInvolvedDetailsModelHotCommentsLikecount];
    self.username = [aDecoder decodeObjectForKey:kZCInvolvedDetailsModelHotCommentsUsername];
    self.childComments = [aDecoder decodeObjectForKey:kZCInvolvedDetailsModelHotCommentsChildComments];
    self.forId = [aDecoder decodeObjectForKey:kZCInvolvedDetailsModelHotCommentsForId];
    self.pid = [aDecoder decodeObjectForKey:kZCInvolvedDetailsModelHotCommentsPid];
    self.content = [aDecoder decodeObjectForKey:kZCInvolvedDetailsModelHotCommentsContent];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_hotCommentsIdentifier forKey:kZCInvolvedDetailsModelHotCommentsId];
    [aCoder encodeObject:_mid forKey:kZCInvolvedDetailsModelHotCommentsMid];
    [aCoder encodeObject:_commentCount forKey:kZCInvolvedDetailsModelHotCommentsCommentCount];
    [aCoder encodeObject:_ishidden forKey:kZCInvolvedDetailsModelHotCommentsIshidden];
    [aCoder encodeObject:_ctype forKey:kZCInvolvedDetailsModelHotCommentsCtype];
    [aCoder encodeObject:_avatar forKey:kZCInvolvedDetailsModelHotCommentsAvatar];
    [aCoder encodeObject:_ctime forKey:kZCInvolvedDetailsModelHotCommentsCtime];
    [aCoder encodeObject:_likecount forKey:kZCInvolvedDetailsModelHotCommentsLikecount];
    [aCoder encodeObject:_username forKey:kZCInvolvedDetailsModelHotCommentsUsername];
    [aCoder encodeObject:_childComments forKey:kZCInvolvedDetailsModelHotCommentsChildComments];
    [aCoder encodeObject:_forId forKey:kZCInvolvedDetailsModelHotCommentsForId];
    [aCoder encodeObject:_pid forKey:kZCInvolvedDetailsModelHotCommentsPid];
    [aCoder encodeObject:_content forKey:kZCInvolvedDetailsModelHotCommentsContent];
}

- (id)copyWithZone:(NSZone *)zone
{
    ZCInvolvedDetailsModelHotComments *copy = [[ZCInvolvedDetailsModelHotComments alloc] init];
    
    if (copy) {

        copy.hotCommentsIdentifier = [self.hotCommentsIdentifier copyWithZone:zone];
        copy.mid = [self.mid copyWithZone:zone];
        copy.commentCount = [self.commentCount copyWithZone:zone];
        copy.ishidden = [self.ishidden copyWithZone:zone];
        copy.ctype = [self.ctype copyWithZone:zone];
        copy.avatar = [self.avatar copyWithZone:zone];
        copy.ctime = [self.ctime copyWithZone:zone];
        copy.likecount = [self.likecount copyWithZone:zone];
        copy.username = [self.username copyWithZone:zone];
        copy.childComments = [self.childComments copyWithZone:zone];
        copy.forId = [self.forId copyWithZone:zone];
        copy.pid = [self.pid copyWithZone:zone];
        copy.content = [self.content copyWithZone:zone];
    }
    
    return copy;
}


@end
